/* internal/js/menu/maintain.js */

/* Common */

var SERVLET_PATH = '/ordrs/internal/maintain-menu'; 

/* END Common */


/* Create */

function createMenu (itemAdded) {
	createMenuValidate(itemAdded);
}

//done
function createMenuValidate (itemAdded) {
	var name = document.getElementById('name').value;
	var description = document.getElementById('description').value;
	var active = document.activityForm.activity[0].checked;

	
	if (name == '') { //left blank
		showFieldRequired('nameMessage');
	}
	else {
		hideFieldMessage('nameMessage');
		createMenuSend(name, description, active, itemAdded);
	}
}

//done
function createMenuSend (name, description, active, itemAdded) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('c') +
	            '&name=' + encodeURI(name) +
	            '&description=' + encodeURI(description) + 
	            '&active=' + encodeURI(active);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				createMenuReceive(req.responseXML, itemAdded);
			} else if (req.status == 204) {
				//can't find server
				createMenuReceive(encodeErrorXML('-1', 'Cannot find server.'), false);
			}
		}
	};
	sendPost(req, url, params);
}

function createMenuReceive (xml, itemAdded) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	var menuID = getErrorDetail(xml);
	if (code == 0) {
		if (itemAdded) {
			//document.getElementById('menuID').value = menuID;
			//addItem();
		}
		else
			reportMenu(menuID);
		showDialog('New Menu created.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Create */

/* Report */

function reportMenu(menuID) {
	reportMenuValidate(menuID);
}

//done
function reportMenuValidate (menuID) {
	try {
		menuID.length;
	} catch(e) {
		menuID = '';
	}
	var name = document.getElementById('name').value;
	var active = document.activityForm.activity[0].checked;
	reportMenuSend(menuID, name, active);
}

//done
function reportMenuSend (menuID, name, active) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('r') +
				'&id=' + encodeURI(menuID) +
				'&name=' + encodeURI(name) +
	            '&active=' + encodeURI(active);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				reportMenuReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				reportMenuReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function reportMenuReceive (xml) {
	if (xml.getElementsByTagName('menus').length > 0) {
		var menus = xml.getElementsByTagName('menus')[0];
		showResults(buildTable(menus));
		if (menus.childNodes.length == 1) {
			var id = menus.childNodes[0].getElementsByTagName('id')[0].childNodes[0].nodeValue;
			showResult(id);
			listItemInMenu(id);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END Report */



/* Update */

function updateMenu() {
	updateMenuValidate();
}

function updateMenuValidate () {
	var id = document.getElementById('menuID').value;
	var name = document.getElementById('name').value;
	var description = document.getElementById('description').value;
	var active = document.activityForm.activity[0].checked;

	if (name == '') { //left blank
		showFieldRequired('nameMessage');
	}
	else {
		hideFieldMessage('nameMessage');
		updateMenuSend(id, name, description, active);
	}
}

//really done
function updateMenuSend (id, name, description, active) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('u') +
	            '&id=' + encodeURI(id) +
	            '&name=' + encodeURI(name) +
	            '&description=' + encodeURI(description) +
	            '&active=' + encodeURI(active);
	
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				updateMenuReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				updateMenuReceive(encodeErrorXML('-1', 'Cannot find server.'), false);
			}
		}
	};
	sendPost(req, url, params);
}


function updateMenuReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		listAllMenuSend();
		showDialog('Menu updated.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Update */


/* Delete */

function deleteMenu() {
	openConfirmModalDialog('Please Confirm',
			'Are you sure you want to delete this Menu?','deleteMenuValidate');
}

function deleteMenuValidate () {
	var id = document.getElementById('menuID').value;
	if (id == '') {
		//no item selected
		showDialog('No Menu is selected for deletion.');
	} else {
		//item selected
		deleteMenuSend(id);
	}
}


function deleteMenuSend (id) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('d') +
	            '&id=' + encodeURI(id);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				/* returned code */
				deleteMenuReceive(req.responseXML);
			} else if (req.status == 204) {
				/* can't find server */
				deleteMenuReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function deleteMenuReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		resetFields();
		listAllMenuSend();
		showDialog('Menu deleted.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Delete */


/* List All */

function listAllMenu () {
	listAllMenuSend();
	listMenuItem();
}

function listAllMenuSend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('l');
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listAllMenuReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listAllMenuReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listAllMenuReceive (xml) {
	if (xml.getElementsByTagName('menus').length > 0) {
		var menus = xml.getElementsByTagName('menus')[0];
		showResults(buildTable(menus));
		if (menus.childNodes.length == 1) {
			var id = menus.childNodes[0].getElementsByTagName('id')[0].childNodes[0].nodeValue;
			showResult(id);
			listItemInMenu(id);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List All */


/* List Item In Menu*/

function listItemInMenu (menuID) {
	listItemInMenuSend(menuID);
}


function listItemInMenuSend (menuID) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('im') +
				 '&id=' + encodeURI(menuID);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listItemInMenuReceive(req.responseXML, menuID);
			} else if (req.status == 204) {
				//can't find server
				listItemInMenuReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listItemInMenuReceive (xml, menuID) {
	if (xml.getElementsByTagName('menuitems').length > 0) {
		var menuitems = xml.getElementsByTagName('menuitems')[0];
		document.getElementById('itemInMenuList').innerHTML = buildMenuItemTable('ItemInMenu',menuitems);
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List Item In Menu */


/* List Menu Item */

function listMenuItem () {
	listMenuItemSend();
}


function listMenuItemSend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('mi');

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listMenuItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listMenuItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listMenuItemReceive (xml) {
	if (xml.getElementsByTagName('menuitems').length > 0) {
		var menuitems = xml.getElementsByTagName('menuitems')[0];
		document.getElementById('menuItemList').innerHTML = buildMenuItemTable('MenuItem', menuitems);
		if (xml.getElementsByTagName('menuitems').length == 0)
			selectMenuItemRow(0);
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List Menu Items */


/* Add an Item to Menu */

function addItem() {
	addItemValidate();
}

function addItemValidate() {
	var menuItemID = document.getElementById('menuItemID').value;
	var menuID = document.getElementById('menuID').value;
	if (menuID == '') {
		//no menu selected, create a new menu
		createMenu(true);
	} else if (menuItemID == '') {
		showDialog('No Menu Item is selected on the right.');
	} else {
		//item selected
		addItemSend(menuID, menuItemID);
	}
}

function addItemSend (menuID, menuItemID) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('ai') +
				 '&menuid=' + encodeURI(menuID) +
				 '&menuitemid=' + encodeURI(menuItemID);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				addItemReceive(req.responseXML, menuID);
			} else if (req.status == 204) {
				//can't find server
				addItemReceive(encodeErrorXML('-1', 'Cannot find server.'), menuID);
			}
		}
	};
	sendPost(req, url, params);
}

function addItemReceive (xml, menuID) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		reportMenu(menuID);
		listMenuItem();
	}
	else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Add an Item to Menu */


/* Remove an Item From Menu */

function removeItem() {
	removeItemValidate();
}

function removeItemValidate() {
	var menuItemID = document.getElementById('itemInMenuID').value;
	var menuID = document.getElementById('menuID').value;
	if (menuID == '') {
		//no menu selected
		showDialog('No Menu is selected.');
	} else if (menuItemID == '') {
		showDialog('No item is selected in the Menu.');
	} else {
		//item selected
		removeItemSend(menuID, menuItemID);
	}
}

function removeItemSend (menuID, menuItemID) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('ri') +
				 '&menuitemid=' + encodeURI(menuItemID);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				removeItemReceive(req.responseXML, menuID);
			} else if (req.status == 204) {
				//can't find server
				removeItemReceive(encodeErrorXML('-1', 'Cannot find server.'), menuID);
			}
		}
	};
	sendPost(req, url, params);
}


function removeItemReceive (xml, menuID) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		reportMenu(menuID);
		listMenuItem();
	}
	else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}


/* END Remove an Item From Menu */

/* Search for a Menu Item */

function searchMenuItem() {
	searchMenuItemValidate();
}

//done
function searchMenuItemValidate (name) {
	var itemNo = document.getElementById('itemNumber').value;
	var name = document.getElementById('itemName').value;
	
	if (itemNo != '' && !isNumeric(itemNo)) // not blank and not numeric
		showFieldMessage('itemNumberMessage','Must be number');
	else {
		hideFieldMessage('itemNumberMessage');
		searchMenuItemSend(itemNo, name);
	}
}

//done
function searchMenuItemSend (itemNo, name) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('si') +
	 			'&number=' + encodeURI(itemNo) +
				'&name=' + encodeURI(name);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				searchMenuItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				searchMenuItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function searchMenuItemReceive (xml) {
	if (xml.getElementsByTagName('menuitems').length > 0) {
		var menuItems = xml.getElementsByTagName('menuitems')[0];
		document.getElementById('menuItemList').innerHTML = buildMenuItemTable('MenuItem', menuItems);
		if (menuItems.childNodes.length == 1) {
			var id = menuItems.childNodes[0].getElementsByTagName('id')[0].childNodes[0].nodeValue;
			selectMenuItemRow(id);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END Search for a Menu Item */


/* Format check */

function isNumeric(strToCheck) {
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strToCheck.length == 0) 
		return false;

	//  test strString consists of valid characters listed above
	for (var i = 0; i < strToCheck.length && blnResult; i++) {
	strChar = strToCheck.charAt(i);
   	if (strValidChars.indexOf(strChar) == -1)
	   blnResult = false;
	}
	return blnResult;
}

/* END Format check */